package com.example.tjremark.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.tjremark.domain.dto.LikeRecordFormDTO;
import com.example.tjremark.domain.dto.LikedTimesDTO;
import com.example.tjremark.domain.po.LikedRecord;
import com.example.tjremark.mapper.LikedRecordMapper;
import com.example.tjremark.service.ILikedRecordService;
import com.tianji.common.autoconfigure.mq.RabbitMqHelper;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.utils.BeanUtils;
import com.tianji.common.utils.CollUtils;
import com.tianji.common.utils.StringUtils;
import com.tianji.common.utils.UserContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 点赞记录表 服务实现类
 * </p>
 *
 * @author yp
 * @since 2024-07-29
 */
//@Service
@RequiredArgsConstructor
@Slf4j
public class LikedRecordServiceImpl2 extends ServiceImpl<LikedRecordMapper, LikedRecord> implements ILikedRecordService {

    private final RabbitMqHelper rabbitMqHelper;

    /**
     * 点赞或者取消点赞
     *
     * @param dto
     */
    @Override
    public void addLikeRecord(LikeRecordFormDTO dto) {
        //1,判断点赞类型，点赞，取消点赞
        boolean success = dto.getLiked() ? like(dto) : unlike(dto);
        //2,判断操作是否成功
        if (success) {
            //3.发送当前的业务的点赞数量
            Integer likeTimes = lambdaQuery()
                    .eq(LikedRecord::getBizId, dto.getBizId())
                    .eq(LikedRecord::getBizType, dto.getBizType())
                    .count();
            log.info("likeTimes-{}", likeTimes);
            if (likeTimes == null) {
                return;
            }
            //发送MQ
            rabbitMqHelper.send(MqConstants.Exchange.LIKE_RECORD_EXCHANGE, StringUtils.format(MqConstants.Key.LIKED_TIMES_KEY_TEMPLATE, dto.getBizType()),
                    new LikedTimesDTO(dto.getBizId(), likeTimes));
        }
    }

    @Override
    public Set<Long> queryListUserIdAndIds(String bizType, List<Long> bizIds) {
        //根据用户id和ids查询点赞的状态,业务类型
        Long userId = UserContext.getUser();
        List<LikedRecord> recordLists = lambdaQuery()
                .eq(LikedRecord::getUserId, userId)
                .in(LikedRecord::getBizId, bizIds)
                .eq(LikedRecord::getBizType, bizType)
                .list();
        if (CollUtils.isEmpty(recordLists)) {
            return CollUtils.emptySet();
        }
        //返回list<Long>的变量表
        return recordLists.stream().map(LikedRecord::getBizId).collect(Collectors.toSet());
    }

    @Override
    public void readLikedTimesAndSendMessage(String biz_type, int maxBizSize) {

    }


    //取消点赞
    private boolean unlike(LikeRecordFormDTO dto) {
        Long userId = UserContext.getUser();
        return lambdaUpdate()
                .eq(LikedRecord::getUserId, userId)
                .eq(LikedRecord::getBizId, dto.getBizId())
                .eq(LikedRecord::getBizType, dto.getBizType())
                .remove();

    }

    //点赞
    private boolean like(LikeRecordFormDTO dto) {
        //1.根据userid，业务id,查询类型查询
        Long userId = UserContext.getUser();
        Integer count = lambdaQuery()
                .eq(LikedRecord::getUserId, userId)
                .eq(LikedRecord::getBizId, dto.getBizId())
                .eq(LikedRecord::getBizType, dto.getBizType())
                .count();
        if (count > 0) {
            return false;
        }
        //2.不存在新增一条记录
        LikedRecord likedRecord = BeanUtils.copyBean(dto, LikedRecord.class);
        likedRecord.setUserId(userId);
        return save(likedRecord);
    }
}
